{extend name="common:base-user" /}
{block name="title"}留言列表- 天佑二手货{/block}
{block name="page"}goods{/block}
{block name="main"}
<div class="am-container am-margin-top-sm">
<div class="am-panel am-panel-default" style="min-height: 480px;">
  <div class="am-panel-hd"><span>留言列表</span><span class="am-fr"><a href="{:url('feedback/add')}" class="">添加</a></span></div>
  <div class="am-panel-bd am-list-news-bd">
        <ul class="am-list am-list-static">
            {foreach $list as $vo} 
            <li class="am-g am-list-item-dated">
                  <a href="#" class="am-list-item-hd" data-id="{$vo['feedback_id']}" style="padding-right: 190px;">{$vo['title']}</a>
                  {if ($vo['is_reply2'] == 1)}
                  <span class="am-list-date "><span class="am-text-warning">New&nbsp;</span>{$vo['create_time']}</span>
                  {else/}
                  <span class="am-list-date">{$vo['create_time']}</span>
                  {/if}
            </li> 
            {/foreach}  
          </ul>
  </div>
</div>
</div>
<div class="am-popup" id="show-detail">
  <div class="am-popup-inner">
    <div class="am-popup-hd">
      <h4 class="am-popup-title"></h4>
      <span data-am-modal-close
            class="am-close">&times;</span>
    </div>
    <div class="am-popup-bd">
    </div>
  </div>
</div>
{/block}

{block name="js"}
<script type="text/javascript">
    $('.am-form').submit(function(event) {
        event.preventDefault();
        $.ajax({
            url: "{:url('MessageBoard/addMessage')}",
            type: 'POST',
            data: $('.am-form').serialize(),
            success: function(data) {
                console.log(data);
                $alert = $('#am-alert');
                $alert.find('.content').text(data.msg);
                $alert.modal('open');
                if(data.code) {
                    setTimeout(function() {
                        location.href = data.url;
                    }, data.wait * 1000);
                } else {
                    $('#captcha-img').click();
                }
            }
        })
    });
    $('.am-list-item-hd').on('click',function(){
        $.get('{:url('feedback/detailByUser')}?id='+$(this).data('id'),function(data){
            if (data.length == 0) {
                $alert = $('#am-alert');
                $alert.find('.content').text('留言不存在或被删除');
                $alert.modal('open');
            }
            $('#show-detail').find('.am-popup-hd h4').html(data['title']);
            var str = '';
            str += '<p>留言内容:';
            str += data['content'];
            str +=  '</p>';
            for (var k = 0; k < data['reply'].length; k++) {
               if(data['reply'][k]['admin_id'] == 0){
                    str += '<p>';
                    str += data['reply'][k]['create_time'];
                    str += ':<span class="am-text-primary">';
                    str += data['reply'][k]['content'];
                    str +=  '</span></p>';
                }else{
                    str += '<p class="am-text-right"><span class="am-text-success">';
                    str += data['reply'][k]['content'];
                    str += '</span>:';
                    str += data['reply'][k]['create_time'];
                    str +=  '</p>';
                }
            }

            if (data['status'] == 1) {
                str += '<hr><form class="am-form" role="form" id="reply-form">\
                  <div class="am-form-group">\
                    <input type="hidden" class="am-form-field" name="feedback_id" value="'+data['feedback_id']+'">\
                    <textarea type="text" class="am-form-field" name="content" rows="3" placeholder="追加留言"></textarea>\
                  </div>\
                  <div class="am-form-group am-form-icon am-form-feedback am-g-collapse am-cf">\
                    <label for="captcha" class="am-u-sm-3 am-form-label">\
                        <div id="captcha-img" title="刷新验证码"><img src="{:captcha_src()}" alt="captcha" /></div>\
                    </label>\
                    <div class="am-u-sm-3">\
                        <input type="text" name="captcha" id="captcha" class="am-form-field" placeholder="请输入验证码">\
                        <!-- <span class="am-icon-warning"></span> -->\
                    </div>\
                  <button type="submit" class="am-btn am-btn-default am-u-sm-3">回复</button>\
                  </div>\
                </form>';
            }else{
                str += '<span class="">已关闭</span>';
            }
            
            $('#show-detail').find('.am-popup-bd').empty();
            $('#show-detail').find('.am-popup-bd').append(str);
            $('#show-detail').modal();
        });
    });
    $(document).on('submit','#reply-form',function(event){
        event.preventDefault();
        $.ajax({
            url: "{:url('feedback/reply')}",
            type: 'POST',
            data: $('#reply-form').serialize(),
            success: function(data) {
                console.log(data);
                $alert = $('#am-alert');
                $alert.find('.content').text(data.msg);
                $alert.modal('open');
                if(data.code) {
                    setTimeout(function() {
                        location.href = data.url;
                    }, data.wait * 1000);
                } else {
                    $alert = $('#am-alert');
                    $alert.find('.content').text(data.msg);
                    $alert.modal('open');
                    $('#captcha-img').click();
                }
            },
            error:function() {
                $('#captcha-img').click();
                $alert = $('#am-alert');
                $alert.find('.content').text('系统错误，请重试');
                $alert.modal('open');
            }
        })
    });
</script>
{/block}
